/***************************************************************************
 创建者：华磊
 开始时间：               : 2020.11.1
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 修改人:华磊
 修改时间:2020.11.28
 修改说明:工程整合

 ***************************************************************************
 *   示教器主程序
 *                                                                         *
 ***************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>

MainWindow::MainWindow(int styleId,  GlobalData *globalDataIn,
                       GlobalData *ioDataIn, QWidget *parent)
    : QMainWindow(parent),
      ui(new Ui::MainWindow)
{
    globalData=globalDataIn;
    ioData=ioDataIn;

#ifdef BUILD_ARM
    deviceDriver = NULL;
#endif

    ui->setupUi(this);
    this->setGeometry(rect().x(), rect().y(),600, 800);
    this->setFixedSize(600, 800);
    //this->setStyleSheet("outline: none;");            //无虚线框

    this->setWindowFlags(Qt::FramelessWindowHint);    //无边框
    this->setAttribute(Qt::WA_QuitOnClose, true);
    initialDisplayStyle(styleId);//修改页面显示风格

    //msgBox
    hmiMsgBox = new HmiMessageBoxDialog;
    ftpProgressDialog = new FtpProgressDialog;

//    keyEvent = new QKeyEvent(QEvent::KeyPress,0,Qt::NoModifier);

    //心跳包
//    heart = new TcpClient;
    //补充 隔膜层 用于topView的退出
    film = new HmiFilmLabel(this);
    film->hide();

}

MainWindow::~MainWindow()
{
    qDebug()<<"*****umount*****";
    if(0==system("umount /agvwork/AgvFundation/fundation"))
    {
        qDebug()<<"*****umount ok*****";
    }
    else
    {
        qDebug()<<"*****umount false*****";
    }

    keyEvent = NULL;
}

void MainWindow::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        //ui->设置所有控件的位置和大小。
        this->setGeometry(rect().x(), rect().y(),600, 800);
        this->setFixedSize(800, 600);
        this->setGeometry(0, 0,800, 600);
        ui->widget->setGeometry(0,0,800,600);
        ui->widget->setStyleSheet("border-image:url(/agvwork/AgvHmi/baseBag/hmiProject/resources/hmiMainresources/main_bakgroud.png)");

        break;
    default:
        break;
    }
}


void MainWindow::setShowInitialLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/mainWindowEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
    hmiMsgBox->changeLanguage();
}

void MainWindow::showMsgBoxTypeSlot(int msgBoxType,QString title,QString text)
{

    emit disableJog_signal();//防止点动按钮由于弹框显示，导致按钮释放消息没有相应。

#ifdef BUILD_ARM
#if QT_VERSION<0x050000
    deviceDriver->inputmethod->HideKeyBoard();//先关闭键盘
#endif
#endif

    hmiMsgBox->hideAllShow();
    if(msgBoxType == EM_MSG_BOX_ALL_BTN)
    {
        hmiMsgBox->messageBoxShowAllBtn(title ,text);
    }
    else if(msgBoxType == EM_MSG_BOX_SURE_BTN)
    {
        hmiMsgBox->messageBoxShowSureBtn(title ,text);
    }
    else if(msgBoxType == EM_MSG_BOX_NO_BTN)
    {
        hmiMsgBox->messageBoxShowWithoutBtn(title ,text);
    }
}

void MainWindow::showMsgBoxNeedReturnTypeSlot(int msgBoxType, int target, QString title, QString text)
{
#ifdef BUILD_ARM
#if QT_VERSION<0x050000
    deviceDriver->inputmethod->HideKeyBoard();//先关闭键盘
#endif
#endif

    hmiMsgBox->messageBoxShowNeedReturn(msgBoxType,target,title,text);
}

void MainWindow::closeMsgBoxSlot()
{
    hmiMsgBox->recvMessageBoxHide();
}

void MainWindow::showFilmSlot()
{
//    if(film)
//    {
//        film->setGeometry(0,0,600,800);
//        film->show();//屏蔽屏幕的虚拟透明膜　hualei todo
//    }
}

void MainWindow::hideFilmSlot()
{
    if(film)
    {
        film->hide();
    }
}

void MainWindow::changeLanguageSlot(int language)
{
    setShowInitialLanguage(language);
}

void MainWindow::showFtpDialog()
{
    ftpProgressDialog->move(50,200);
    ftpProgressDialog->show();
}

int MainWindow::shortCutSetStop()
{
    // 空格:暂停　　(空格键值32)
    if(keyEvent->key() == Qt::Key_Space)
    {
        emit pauseRun_signal();
        showMsgBoxTypeSlot(EM_MSG_BOX_SURE_BTN,tr("警告"),tr("暂停按键被按下,AGV暂停运行!"));
        return 1;
    }
    // 回车:停止    (回车键值13)这里回车键值为其他值，具体原因不知
    else if(keyEvent->text()[0] == '\r')
    {
        emit stopRun_signal();
        showMsgBoxTypeSlot(EM_MSG_BOX_SURE_BTN,tr("警告"),tr("停止按键被按下,AGV停止运行!"));
        return 1;
    }
    return 0;
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    switch(event->type())
    {
    case QEvent::MouseButtonPress:
        //case QEvent::KeyPress:
    case QEvent::TouchBegin:
    {
//        int nMiniDlgCurrentId = EM_DIF_HIDE_ALL;
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();

//        m_pCustomInstace->m_emPressFlag = EM_PDU_DOWN;
//        m_pCustomInstace->m_emMouseFlag = EM_PDU_DOWN;

        ReleasePos.setX(ReleasePos.x()-70);
        ReleasePos.setY(ReleasePos.y()-26);

        /**键盘显示 start*/
        //2020.11.24 修改 支持按键点击空白处退出
        //QWidget *current_focus_widget;
        QMainWindow *current_focus_widget;
        //current_focus_widget = QApplication::focusWidget();//(QWidget *)obj;
        current_focus_widget = (QMainWindow *)obj;
        if(current_focus_widget->objectName().compare("qt_scrollarea_viewport") == 0)
        {
            current_focus_widget = (QMainWindow *)current_focus_widget->parent();
        }


        // 如果是锁屏
//        if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_LOCK_SCREEN_FLAGE] == EM_CF_ON)
//        {
//            return true;
//        }
//        if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_TERMINATE_OVER_LAYER] == EM_CF_ON)
//        {
//            return true;
//        }

        //速度滚动条
//        if(ui->speedSlider)
//        {
//            if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_LOGIN_POWER_SHOW_CTRL] == EM_CF_ON)
//            {
//                if(ui->speedSlider->geometry().contains(ReleasePos))
//                {
//                    m_pCustomInstace->m_pnAllFlagList[EM_AFT_SLIDER_SET_FLAG] = EM_CF_ON;
//                }
//            }
//        }

        if(!current_focus_widget)
        {
            return QWidget::eventFilter(obj, event);
        }
        if(current_focus_widget->inherits("QPushButton"))
        {

            #ifdef BUILD_ARM
            deviceDriver->setBuzzerPlay_slot();
            #endif

            #ifdef BUILD_ARM
            #if QT_VERSION<0x050000
            if(!deviceDriver->inputmethod->GetMethodRect().contains(ReleasePos))
            {
                //system("echo 0 >> /root/1.txt");
                //deviceDriver->inputmethod->HideKeyBoard();
            }
            #endif
            #endif
        }
        else
        {
            #ifdef BUILD_ARM
            if(current_focus_widget)
            {
                if(current_focus_widget->inherits("QLineEdit") || current_focus_widget->inherits("QTextEdit"))
                {
                    deviceDriver->setBuzzerPlay_slot();

                    #if QT_VERSION<0x050000
                    deviceDriver->inputmethod->ShowKeyBoard(obj);
                    #endif
                }
                else
                {

                    #if QT_VERSION<0x050000
                    //system("echo  0 >> /root/2.txt");
                    deviceDriver->inputmethod->HideKeyBoard();
                    #endif
                }
            }
            #endif
        }

        /**键盘显示 end*/

        if(current_focus_widget->inherits("HmiFilmLabel"))//自定义的一层隔膜
        {
            //qDebug()<<"main window";
            hideFilmSlot();

            emit hideAllTopView();
        }

        /**模式选择页面显示隐藏控制 start*/
//        nMiniDlgCurrentId = m_pCustomInstace->m_psDialogTable->m_emMiniCurrentShowId;
//        if(m_pCustomInstace->m_psDialogTable->m_sDialogInfo[nMiniDlgCurrentId].m_pWindowDialog)
//        {
//            if(!m_pCustomInstace->m_psDialogTable->m_sDialogInfo[nMiniDlgCurrentId].m_pWindowDialog->geometry().contains(ReleasePos))
//            {
//                m_pCustomInstace->m_psDialogTable->m_sDialogInfo[nMiniDlgCurrentId].m_pWindowDialog->hide();
//                m_pCustomInstace->m_psDialogTable->m_emMiniCurrentShowId = EM_DIF_HIDE_ALL;
//            }
//        }
        /**模式选择页面显示隐藏控制 end*/

//        if(MotionCtlDlg != NULL)
//        {

//            QRect rcRunBt = ui->RunBtn->geometry();
//            if((ReleasePos.x()>rcRunBt.x()) && (ReleasePos.x()<(rcRunBt.x()+rcRunBt.width())))
//            {
//                if((ReleasePos.y()>rcRunBt.y()) && (ReleasePos.y()<(rcRunBt.y()+rcRunBt.height())))
//                {
//                    m_pCustomInstace->m_pnAllFlagList[EM_AFT_RUN_RELEASE_CTRL_FLAGE] = EM_CF_OFF;
//                    emit sig_RunProBtRunEvent(EM_UKT_STATE_PRESS);
//                }
//            }
//        }

    }
        break;
    case QEvent::MouseButtonRelease:
        //case QEvent::KeyRelease:
    case QEvent::TouchEnd:
    {
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();

        //
//        m_pCustomInstace->m_emPressFlag = EM_PDU_UP;
//        m_pCustomInstace->m_emMouseFlag = EM_PDU_UP;

        ReleasePos.setX(ReleasePos.x()-70);
        ReleasePos.setY(ReleasePos.y()-26);

        // 如果是锁屏
//        if((m_pCustomInstace->m_pnAllFlagList[EM_AFT_LOCK_SCREEN_FLAGE] == EM_CF_ON)
//                ||(m_pCustomInstace->m_pnAllFlagList[EM_AFT_TERMINATE_OVER_LAYER] == EM_CF_ON))
//        {
//            m_pCustomInstace->m_pnAllFlagList[EM_AFT_SLIDER_SET_FLAG] = EM_CF_OFF;
//            return true;
//        }
        // 速度设置滚动条
//        if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_SLIDER_SET_FLAG] == EM_CF_ON)
//        {
//            slots_on_speedSlider_sliderReleased();
//        }
//        if(MotionCtlDlg)
//        {
//            QRect rcRunBt = ui->RunBtn->geometry();
//            if((ReleasePos.x()>rcRunBt.x()) && (ReleasePos.x()<(rcRunBt.x()+rcRunBt.width())))
//            {
//                if((ReleasePos.y()>rcRunBt.y()) && (ReleasePos.y()<(rcRunBt.y()+rcRunBt.height())))
//                {
//                    emit sig_RunProBtRunEvent(EM_UKT_STATE_RELEASE);
//                }
//            }
//            if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_RUN_RELEASE_CTRL_FLAGE] == EM_CF_OFF)
//            {
//                m_pCustomInstace->m_pnAllFlagList[EM_AFT_RUN_RELEASE_CTRL_FLAGE] = EM_CF_ON;
//            }
//        }
        // 模板限制修改
    }
        break;
    case QEvent::MouseButtonDblClick:
    {
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();

        ReleasePos.setX(ReleasePos.x()-70);
        ReleasePos.setY(ReleasePos.y()-26);

//        if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_LOCK_SCREEN_FLAGE] == EM_CF_ON)
//        {
//            return true;
//        }
//        if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_TERMINATE_OVER_LAYER] == EM_CF_ON)
//        {
//            return true;
//        }

    }
        break;
    case QEvent::MouseMove:
    {
        QPoint ReleasePos;
        ReleasePos = QCursor::pos();
//        if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_LOCK_SCREEN_FLAGE] == EM_CF_ON)
//        {
//            return true;
//        }
//        if(m_pCustomInstace->m_pnAllFlagList[EM_AFT_TERMINATE_OVER_LAYER] == EM_CF_ON)
//        {
//            return true;
//        }
    }
        break;
    case QEvent::KeyPress:
    {
        if(globalData->globalCurrentState == ENUM_COMMAND_RUN)
        {
            keyEvent = static_cast<QKeyEvent*>(event);
            // 键盘按下,处理空格暂停和回车急停
            if(shortCutSetStop())
            {
                return true;
            }
        }

//        if(globalData->globalCurrentState != ENUM_COMMAND_RUN)
//        {
//           keyEvent = static_cast<QKeyEvent*>(event);
//           if(keyEvent->key() == Qt::Key_Space)
//           {
//               return true;
//           }
//        }

    }
        break;
    default:
        break;

    }
    return QWidget::eventFilter(obj, event);
}


